package com.qigao.mall.web.controller.admin.merchant;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.ItemCatService;
import com.qigao.mall.commons.dto.*;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.web.controller.app.user.AppItemCatController;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.app.ItemCatCondVo;
import com.qigao.mall.web.vo.app.ItemCatCreateVo;
import com.qigao.mall.web.vo.app.ItemCatModiVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/admin/itemCat")
@Api(tags = "Admin商户端商品分类相关API",value = "App商户端商品分类相关API")
public class AdminItemCatController extends AppItemCatController {

    private static Logger logger= LoggerFactory.getLogger(AdminItemCatController.class);
    @Resource
    ItemCatService itemCatService;

    //新增商品分类
    @PostMapping("/create")
    public Result create(@RequestBody ItemCatCreateVo itemCatCreateVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(itemCatCreateVo);
        try{
            ItemCatCreateDto itemCatCreateDto=new ItemCatCreateDto();
            BeanUtils.copyProperties(itemCatCreateVo,itemCatCreateDto);
            String itemSort= StringUtils.isEmpty(itemCatCreateVo.getItemSort())?"1":itemCatCreateVo.getItemSort();
            itemCatCreateDto.setItemSort(Integer.valueOf(itemSort));
            itemCatCreateDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            itemCatService.create(itemCatCreateDto);
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("新增商品分类发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    //修改商品分类
    @PostMapping("/modi")
    public Result create(@RequestBody ItemCatModiVo itemCatModiVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(itemCatModiVo);
        try{
            ItemCatModiDto itemCatModiDto=new ItemCatModiDto();
            BeanUtils.copyProperties(itemCatModiVo,itemCatModiDto);
            String itemSort= StringUtils.isEmpty(itemCatModiVo.getItemSort())?"1":itemCatModiVo.getItemSort();
            itemCatModiDto.setItemSort(Integer.valueOf(itemSort));
            itemCatModiDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            itemCatService.modi(itemCatModiDto);
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("新增商品分类发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //删除商品分类
    @PostMapping("/del/{itemCatNo}")
    public Result create(@PathVariable("itemCatNo")String itemCatNo){
        return itemCatService.deleteByItemCatNo(itemCatNo);
    }

    @RequestMapping(value = "listPage", method = RequestMethod.POST)
    @ApiOperation(value = "分类分页查询", notes = "分类分页查询")
    @ApiParam(name = "itemCatCondiVo", value = "分类分页查询", required = true)
    public Result listPage(@RequestBody ItemCatCondVo itemCatCondiVo) {
        ValidatorUtil.validateEntity(itemCatCondiVo);//校验
        try {
            ItemCatCondDto itemCatCondDto = new ItemCatCondDto();
            BeanUtils.copyProperties(itemCatCondiVo, itemCatCondDto);
            Page<ItemCatResultDto> itemCatResultDtoPage = itemCatService.listPage(itemCatCondDto);
            PageInfo pageInfo = new PageInfo(itemCatResultDtoPage);
            return Result.newSuccess(pageInfo);
        } catch (Exception e) {
            logger.error("分类分页查询异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @RequestMapping(value = "listTree", method = RequestMethod.POST)
    @ApiOperation(value = "分类树状展示", notes = "分类树状展示")
    public Result listTree() {
        try {
            List<ItemCatResultDto> itemCatResultDtos = itemCatService.listTree();
            return Result.newSuccess(itemCatResultDtos);
        } catch (Exception e) {
            logger.error("分类树状展示异常!{}", e);
            return Result.newError(ResultCode.FAIL);
        }
    }

}
